import * as types from "../types/loginTypes";

// 访问登录接口 根据返回结果来划分action属于哪个type,然后返回对象,给reducer处理

function isLogining() {
  return {
    type: types.LOGIN_IN_DOING,
  };
}

function loginSuccess(isSuccess, user) {
  console.log("success");
  return {
    type: types.LOGIN_IN_DONE,
    user,
  };
}

function loginError(isSuccess) {
  console.log("error");
  return {
    type: types.LOGIN_IN_ERROR,
  };
}

export default function login() {
  return dispatch => {
    dispatch(isLogining());
    // 模拟用户登录
    const result = fetch("https://www.baidu.com/")
      .then(res => {
        dispatch(loginSuccess(true));
      })
      .catch(e => {
        dispatch(loginError(false));
      });
  };
}
